package com.bro.person.admin.websocket;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.messaging.simp.stomp.StompHeaderAccessor;
import org.springframework.web.socket.messaging.SessionConnectEvent;

/**
 *
 * STOMP 监听类
 * 用于session注册以及key值获取
 * @author swallow
 * Created by swallow on 2018/10/9.<br>
 */
public class STOMPConnectEventListener implements ApplicationListener<SessionConnectEvent> {

    private static final Logger logger = LoggerFactory.getLogger(STOMPConnectEventListener.class);

    @Autowired
    private SocketSessionRegistry webAgentSessionRegister;

    @Override
    public void onApplicationEvent(SessionConnectEvent event) {
        StompHeaderAccessor sha = StompHeaderAccessor.wrap(event.getMessage());
        String agentId = sha.getNativeHeader("login").get(0);
        String sessionId = sha.getSessionId();
        logger.info("[STOMPConnectEventListener][onApplicationEvent] 接收的sessionId是： event:{},sessionId:{},agentId:{}",event,sessionId,agentId);
        webAgentSessionRegister.registerSessionId(agentId,sessionId);
    }
}
